/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-27
 * Time: 18:41
 */
class Node{
    public int val;
    public Node next;
    public Node(int val){
        this.val = val;
    }
}

public class MyQueue {
    public Node head;
    public Node last;

    /**
     * 入队
     * @param val 插入的元素
     */
    public void offer(int val){
        Node node = new Node(val);
        if(this.head==null){
            this.head = node;
            this.last = node;
        }else {
            last.next = node;
            last = last.next;
        }
    }

    /**
     * 出队
     * @return 返回出队的元素
     */
    public int poll(){
        if(isEmpty()){
            throw new NullPointerException("队列为空");
        }
        int oldVal = this.head.val;
        this.head = this.head.next;
        return oldVal;
    }

    /**
     * 判断队列是否为空
     * @return 空返回 true，否则返回false
     */
    public boolean isEmpty(){
        return this.head==null;
    }

    /**
     * 获取队头元素
     * @return 返回队头元素
     */
    public int peek(){
        if(isEmpty()){
            throw new NullPointerException("队列为空");
        }
        return this.head.val;
    }
}
